********************************************************************************
** *TABLE 3: Take-up (HIGH HOPES)
********************************************************************************

set more off
use "$merge_mobile", clear 



**create a local for  the Ind variables for Table 3 regression table (column 3-8)
local deposit deposit_2_jan31 deposit_trans_mba_31 deposit_3_jan31  deposit_trans_lsa_31 loan_disb_sd_jan31 deposit_nb_loan_jan31 


*Column 1 & 2
* Mshwari instrumented with t1 and t2 : all sample 
xi: regress mshwari_after mshwariorlsa treatment2 i.stratification b_primaryormore b_secondaryormore b_age_imp b_age_imp2 b_age_imp_dummy  b_mpesastatus nb_children_imp, vce(cluster b_schoolname_baseline_encoded)
summ mshwari_after if e(sample)==1 & control==1 
estadd local cmean=string(`r(mean)', "%9.3f")
estadd local fstat=string(`e(F)')
estadd local R2a = string(e(r2_a), "%9.2f")

estimates store mshwari_all

* LSA instrumented with t1 and t2 : all sample 
xi: regress lsa_after mshwariorlsa treatment2 i.stratification b_primaryormore b_secondaryormore b_age_imp b_age_imp2 b_age_imp_dummy  b_mpesastatus nb_children_imp, vce(cluster b_schoolname_baseline_encoded)
summ lsa_after if e(sample)==1 & control==1 
estadd local cmean=string(`r(mean)', "%9.3f")
estadd local fstat=string(`e(F)')
estadd local R2a = string(e(r2_a), "%9.2f")

estimates store lsa_all


*Reg Column 3-8
foreach var of varlist  `deposit' {	

	*** Mshwari instrumented with treatment1 and treatment2 ; all sample
	
	*ITT
	xi: reg `var' mshwariorlsa treatment2 i.stratification b_primaryormore b_secondaryormore b_age_imp b_age_imp2 b_age_imp_dummy b_mpesastatus nb_children_imp, cluster(b_schoolname_baseline_encoded) 
		summ `var' if e(sample)==1 & control==1 
		estadd local R2 = string(e(r2), "%9.2f")
		estadd local R2a = string(e(r2_a), "%9.2f")
		estadd local cmean=string(r(mean), "%9.2f")
		estimates store `var'_ITT		
	*TOT/LATE Panel
	xi: ivreg2 `var' (mshwari_after lsa_after=mshwariorlsa treatment2) i.stratification b_primaryormore b_secondaryormore b_age_imp b_age_imp2 b_age_imp_dummy b_mpesastatus nb_children_imp, cluster(b_schoolname_baseline_encoded) 
		estadd local Papp=string(e(widstat), "%9.0f")
		estadd local PappLM=string(e(idstat), "%9.0f")
		estadd local R2 = string(e(r2), "%9.2f")
		estadd local R2u = string(e(r2u), "%9.2f")
		estadd local R2a = string(e(r2_a), "%9.2f")
		summ `var' if e(sample)==1 & control==1 
		estadd local cmean=string(r(mean), "%9.2f")
		estimates store `var'_TOT
	
}


	*ITT
	local tablist "mshwari_all lsa_all"
	foreach var in `deposit' {
		local i `var'_ITT 
		local tablist "`tablist' `i'"
		}
	
	display "`tablist'"
	#delimit ;
			esttab `tablist' using "$output/Table_03_Deposits_Credit_PanelA.csv",
			replace 
			nolines nogaps nonotes nomtitles nodepvars noobs
			drop(_Istratific_* _cons)
			scalars( "R2a")
			b(%9.2f) se(%9.2f) 
			starlevels(* 0.1 ** 0.05 *** 0.01)
			obslast legend label collabels(none) 
			mgroups("MBA Adoption" "LSA Adoption" "MBA Deposits" "Number of MBA Deposits" "LSA Deposits" "Number of LSA Deposits"  "Total Amount of MBA Loan" "Number of MBA Loans"   , pattern(1 1 1 1 1 1 1 1 ) );
	#delimit cr	

	
	local tablist ""
	foreach var in `deposit' {
		local i `var'_TOT 
		local tablist "`tablist' `i'"
		}
	* TOT
	#delimit ;
		esttab `tablist' using "$output/Table_03_Deposits_Credit_panelB.csv",
		replace 
		nolines nogaps nonotes nomtitles nodepvars 
		drop(_Istratific_* _cons)
		scalars("Papp F-Statistic for weak identification"  
		"R2a" "cmean Control Mean")
		b(%9.2f) se(%9.2f) 
		starlevels(* 0.1 ** 0.05 *** 0.01)
		obslast legend label collabels(none) 
		mgroups("MBA Deposits" "Number of MBA Deposits" "LSA Deposits" "Number of LSA Deposits"  "Total Amount of MBA Loan" "Number of MBA Loans"   , pattern(1 1 1 1 1 1 ) );
	#delimit cr	




**calculate MBA/LSA complier's' mean in control

**MBA 	
gen mba=.
replace mba=1 if treatment_arm==1 | treatment_arm==2
replace mba=0 if treatment_arm==0

	//syntax comp_mean_TOT dep_var treat_var compliance_var
foreach var of varlist `deposit' {
	
	comp_mean_TOT `var' mba mshwari_after 
	
}	

drop lsa //
gen lsa = .
replace lsa=1 if treatment_arm==2
replace lsa=0 if treatment_arm==0

foreach var of varlist `deposit' {
	
	comp_mean_TOT `var' lsa lsa_after 
	
}	
	
	
exit 

Notes: MBA exposure =1 if MBA treatment=1 OR LSA treatment=1.   LSA exposure=1 if MBA treatment=0 and LSA treatment=1.  The coefficient on LSA exposure reflects the marginal impact of exposure to the LSA relative to exposure to the MBA.  All outcome variables calculated using administrative data available between recruitment and January 31. Sample restricted to respondents who provided ADS consent. Robust standard errors clustered at school level.  PPP USD equivalent for KsH coefficients is given in square brackets in column 1, 3 and 5 Control variables include number of children in the household, a quadratic in respondent's age and indicator variables for primary, secondary school completion and M-PESA use at baseline.  *** significant at 1%, ** significant at 5%, * significant at 10%.